const Controller = require('egg').Controller;

let result = { };

class UserController extends Controller {

    async list() {
        const ctx = this.ctx;
        try {
            const list = await ctx.service.user.list();
            if (list) {
                result = {
                    code: 2000,
                    msg: '用户列表查询成功！',
                    data: list
                }
            } else {
                result = {
                    code: 3000,
                    msg: '暂无用户数据！',
                    data: null
                }
            }
        } catch (error) {
            result = {
                code: 5000,
                msg: '网络开小差了， 请稍后重试！',
            }
            throw error;
        }
        this.ctx.body = result;
    }
    

    async getOne() {
        const ctx = this.ctx;
        const list = await ctx.service.user.list();
        this.ctx.body = list;
    }


    async create() {
        const ctx = this.ctx;
        const list = await ctx.service.user.list();
        this.ctx.body = list;
    }


    async update() {
        const ctx = this.ctx;
        const list = await ctx.service.user.list();
        this.ctx.body = list;
    }


    async login() {
        const ctx = this.ctx;
        const user = ctx.query;
        try {
            const res = await ctx.service.user.login(user);
            if (res) {
                result = {
                    code: 2000,
                    msg: '用户登录成功！',
                    data: res
                }
            } else {
                result = {
                    code: 3000,
                    msg: '当前用户不存在或用户名密码错误！',
                    data: res
                }
            }
        } catch (error) {
            result = {
                code: 5000,
                msg: '网络开小差了， 请稍后重试！',
            }
            throw error;
        }
        this.ctx.body = result;
    }
}


module.exports = UserController;